<?php
class songs {
    
    function __construct() {
        
    }

    /**
     * 1专辑
     * 3精选集
     */
    public function getSongId() {
        $listIdArr = array(
            /*1 => array(
                '347661',
            ),*/
            3 => array(
                '12306306',
                '1770379867',
            )
        );
        $url = 'http://www.xiami.com/song/playlist/id/';
        $param = '/cat/json';
        $isong = array();
        foreach ($listIdArr as $type => $listIds) {
            foreach ($listIds as $key => $listId) {
                $songData = $this->curlGet($url.$listId."/type/".$type.$param);
                $songArr = json_decode($songData, true);
                if($songArr['data']['trackList']) {
                    foreach ($songArr['data']['trackList'] as $k => $songlist) {
                        $isong[] = $songlist['song_id'];    
                    }
                }
            }
        }
        if(!empty($isong)) {
            $sKey = array_rand($isong);
            return $isong[$sKey];   
        } else {
            return "1768955553";
        }
    }

    private function curlGet($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}